import os
import time
from multiprocessing import Pool
from random import random


def task(name):
    print('任务开始...')
    start = time.time()
    time.sleep(random() * 2)
    end = time.time()
    # print('任务：{}结束，用时：{}，进程号：{}'.format(name, (end - start), os.getpid()))
    return '任务：{}结束，用时：{}，进程号：{}'.format(name, (end - start), os.getpid())


container = []


def callback_func(n):
    container.append(n)


if __name__ == '__main__':
    pool = Pool(5)
    task_list = ['打游戏', '做饭', '看电视', '听音乐', '吃饭', '学python', '学Java']
    for t in task_list:
        pool.apply_async(task, args=(t,), callback=callback_func)
    pool.close()
    pool.join()
    for c in container:
        print(c)
    print('over...')
